package commands

import (
	"os"
	"text/template"

	"github.com/urfave/cli"

	"../flags"
	"../utils"
)

// TaskList - lookup task
func TaskList(config utils.Config) cli.Command {
	return cli.Command{
		Name: "tasklist",
		Aliases: []string{
			"tl",
		},
		Usage:    "lookup task, default for active task",
		Category: "DEVELOP COMMANDS",
		Flags: []cli.Flag{
			flags.Flags["path"],
			cli.BoolFlag{
				Name:  "own, o",
				Usage: "see own tasks",
			},
			cli.BoolFlag{
				Name:  "all, a",
				Usage: "see all tasks",
			},
			cli.BoolFlag{
				Name:  "inactive, i",
				Usage: "only see inactive tasks",
			},
			cli.BoolFlag{
				Name:  "closed, c",
				Usage: "only see closed tasks",
			},
		},
		Action: makeAction(config, tasklistAction),
	}
}

/*
default -> active, unclosed, all-user
--all --own -> all, all, own
--all -> all, all, all
--inactive -> inactive, unclosed, all
--closed -> active, closed, all
*/

func tasklistAction(conf utils.Config, git utils.Git, context *cli.Context) error {
	configResult, err := utils.LoadConfig(conf, git, "config")
	if err != nil {
		return err
	}
	var ownName string
	ownName, err = configResult.String("session", conf.PcCode)
	if err != nil {
		ownName = "[no login]"
	}

	own := context.Bool("own")
	all := context.Bool("all")
	active := !context.Bool("inactive")
	closed := context.Bool("closed")

	// fmt.Printf("%t %t %t %t", own, all, active, closed)

	result, err := utils.LoadConfig(conf, git, "log")
	if err != nil {
		return err
	}

	var tasks []string
	for _, item := range result.Sections() {
		if ("DEFAULT" == item) || "common" == item {
			continue
		}
		_closed := result.HasOption(item, "finish_time")
		_active, err := result.Bool(item, "active")
		if err != nil {
			return err
		}
		worker, err := result.String(item, "worker")
		if err != nil {
			return err
		}
		_own := worker == ownName

		/*
			if own {
				if _own {
					// own=true & _own=true
					if all {
						tasks = append(tasks, item)
					} else if (active == _active) && (closed == _closed) {
						tasks = append(tasks, item)
					}
				}
				// own=true & _own=false
				// pass
			} else {
				// own=false & _own=false/true
				if all {
					tasks = append(tasks, item)
				} else if (active == _active) && (closed == _closed) {
					tasks = append(tasks, item)
				}
			}
		*/
		/*
			if !(own && !_own) {
				if all {
					tasks = append(tasks, item)
				} else if (active == _active) && (closed == _closed) {
					tasks = append(tasks, item)
				}
			}
		*/
		if !(own && !_own) && (all || ((active == _active) && (closed == _closed))) {
			tasks = append(tasks, item)
		}
	}

	var data []TaskItem
	for _, item := range tasks {
		NAME, _ := result.String(item, "name")
		MESSAGE, _ := result.String(item, "message")
		PUBLISHER, _ := result.String(item, "publisher")
		WORKER, _ := result.String(item, "worker")

		BRANCH, _ := result.String(item, "branch")
		ACTIVE, _ := result.String(item, "active")
		CT, _ := result.String(item, "create_time")
		FT, _ := result.String(item, "finish_time")
		FU, _ := result.String(item, "finish_user")

		data = append(data, TaskItem{
			Tag:       item,
			Name:      NAME,
			Message:   MESSAGE,
			Publisher: PUBLISHER,
			Worker:    WORKER,
			Branch:    BRANCH,
			Active:    ACTIVE,
			Ct:        CT,
			Ft:        FT,
			Fu:        FU,
		})
	}

	tmpl, err := template.New("Task List").Parse(TaskListTemplate)
	if err != nil {
		return err
	}
	return tmpl.Execute(os.Stdout, TaskContainer{
		List: data,
	})
}

// TaskContainer - Task Container
type TaskContainer struct {
	List []TaskItem
}

// TaskItem - Task Item
type TaskItem struct {
	Tag       string
	Name      string
	Message   string
	Publisher string
	Worker    string
	Branch    string
	Active    string
	Ct        string
	Ft        string
	Fu        string
}

// TaskListTemplate - Task List Template
var TaskListTemplate = `
--------- [  Task  List  ] ---------
| TAG | Name | Message | Publisher | Worker | Branch | Active | Create Time | Finish Time | Finish User |
{{range .List}}|{{.Tag}}|{{.Name}}|{{.Message}}|{{.Publisher}}|{{.Worker}}|{{.Branch}}|{{.Active}}|{{.Ct}}|{{.Ft}}|{{.Fu}}|
{{end}}
------------------------------------
`

/*
[task-ID]
name = NAME
message = MESSAGE
publisher = ownName
worker = WORKER
# TYPE = hotfixes/release/feature
type = TYPE
create_time = TIME
branch = BRANCH
active = true/false
finish_time = [time]
finish_user = [no user]
*/
